<template>
  <div class="login_container">
    <el-form
      :model="loginForm"
      :rules="loginFormRules"
      ref="loginFormRef"
      label-width="100px"
    >
      <el-form-item prop="userName" label="用户名:">
        <el-input
          v-model="loginForm.userName"
          placeholder="请输入用户名"
          type="test"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">登录</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  props: [],
  data() {
    return {
      loginForm: {
        userName: "admin",
        password: "123456",
      },
      loginFormRules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 4, max: 6, message: "长度在 4 到 6 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    async submitForm() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid)
          return this.$message.info("请输入完成且符合规则的用户名和密码");
        const res = await this.$store.dispatch("userLogin", this.loginForm);
        if (res !== 1) return this.$message.error("登陆失败");
        this.$router.push("/dashboard");
        this.$message.success("登陆成功，欢迎" + this.loginForm.userName);
      });
    },
    reset() {
      this.$refs.loginFormRef.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
</style>